/**
 * @author 黄波
 * 创建日期：2011Sep 19, 2011
 * TODO// N/A
 */
package com.platform.res.action;

import java.util.ArrayList;
import java.util.List;

import com.platform.bean.PageBean;
import com.platform.os.util.Util;
import com.platform.pojo.ResInfo;
import com.platform.pojo.ResType;
import com.platform.pojo.UserInfo;
import com.platform.res.bean.ResInfoBean;
import com.platform.res.bo.ResBo;
import com.platform.user.bo.UserBo;

/**
 * 分页查询资源信息列表
 */
public class LoadResInfoListAction extends ResourceSupport {

	private PageBean pageBean;
	private List<ResInfoBean> items;
	private String nodeId;
	private String nodeText;
	public PageBean getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	public List<ResInfoBean> getItems() {
		return items;
	}
	public void setItems(List<ResInfoBean> items) {
		this.items = items;
	}
	public String getNodeId() {
		return nodeId;
	}
	public void setNodeId(String nodeId) {
		this.nodeId = nodeId;
	}
	public String getNodeText() {
		return nodeText;
	}
	public void setNodeText(String nodeText) {
		this.nodeText = nodeText;
	}
	@Override
	public String execute() throws Exception {
		ResBo bo = ResBo.getInstance();
		Object[] objs = bo.getInfos(pageBean);
		pageBean = (PageBean)objs[0];
		List<ResInfo> resInfos = (List)objs[1];
		items = new ArrayList<ResInfoBean>();
		//获得所有的类型
		List<ResType> types = bo.getTypes();
		
		//用户bo
		UserBo userBo = UserBo.getInstance();
		for(ResInfo item : resInfos) {
			ResInfoBean bean = new ResInfoBean();
			bean.setInfo(item);
			if(types != null) {
				ResType tmp = getTypeName(item.getTypeId(), types);
				if(tmp != null) {
					bean.setTypeName(tmp.getName());
				} else {
					bean.setTypeName("课程资源");
				}
			}
			UserInfo user = userBo.getUser(item.getUploadUserId().toString());
			if(user != null) {
				bean.setUserName(user.getRealName());
			} else {
				bean.setUserName(null);
			}
			bean.setUploadTime(Util.parseDate(item.getUploadTime(), "yyyy-MM-dd HH:mm:ss"));
			items.add(bean);
		}
		if(nodeText != null) {
			nodeText = new String(nodeText.getBytes("iso-8859-1"), "utf-8");
		}
		return SUCCESS;
	}
	/**
	 * @param typeId
	 * @return
	 */
	private ResType getTypeName(Integer typeId, List<ResType> types) {
		for(ResType item : types) {
			if(item.getId() == typeId) {
				return item;
			}
		}
		return null;
	}
	
	
	
}
